package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import cn.wolfcode.luowowo.website.web.util.UMEditorUploader;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//游记Controller
@Controller
@RequestMapping("/travel")
public class TravelController {

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommentService travelCommentService;

    //游记首页(分页操作)
    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") TravelQuery qo){

        model.addAttribute("pageInfo",travelService.query(qo));

        return "travel/list";
    }

    //游记明细
    @RequestMapping("/detail")
    public String detail(Model model,Long id,@UserParam UserInfo userInfo){

        //detail
        Travel travel = travelService.get(id);
        TravelContent travelContent = travelService.getContent(id);
        travel.setTravelContent(travelContent);
        model.addAttribute("detail",travel);
        //共享当前登录用户的id
        if(userInfo != null){
            model.addAttribute("userId",userInfo.getId());
        }

        //toasts
        List<Destination> toasts = destinationService.getToasts(travel.getDest().getId());
        model.addAttribute("toasts", toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);

        //sds 攻略明细前三
        model.addAttribute("sds",strategyDetailService.getViewnumTop3(travel.getDest().getId()));

        //ts  游记前三
        model.addAttribute("ts",travelService.getViewnumTop3(travel.getDest().getId()));

        //游记的评论
        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setCurrentPage(1); //从0开始,传入1代表从索引0开始,里面的参数会-1
        qo.setPageSize(Integer.MAX_VALUE);//最大值,表示没有分页,一页显示全部
        Page page = travelCommentService.query(qo);

        model.addAttribute("list",page.getContent());

        return "travel/detail";
    }

    // 编辑
    @RequestMapping("/input")
    public String input(Model model,Long id){

        //tv
        if(id != null){
            Travel travel = travelService.get(id);
            travel.setTravelContent(travelService.getContent(id));
            model.addAttribute("tv",travel);
        }

        //dests 深度-国家城市省份
        model.addAttribute("dests",destinationService.getDestsByDeep(3));

        return "travel/input";
    }

    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    //保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo){
        //作者
        travel.setAuthor(userInfo);

        Long id = travelService.saveOrUpdate(travel);

        return AjaxResult.success().addData(id);
    }

    //测试没贴注解是否会注入属性
    @RequestMapping("/update")
    @ResponseBody
    public Object update(UserInfo userInfo){
        return userInfo;
    }

    //这方法会注入属性
    @RequestMapping("/info")
    @ResponseBody
    public Object info(@UserParam UserInfo userInfo){
        return userInfo;
    }

    //评论添加
    @RequestMapping("/commentAdd")
    public String commentAdd(Model model,TravelComment comment,int floor,@UserParam UserInfo UserInfo){

        comment.setCity(UserInfo.getCity());
        comment.setHeadUrl(UserInfo.getHeadImgUrl());
        comment.setLevel(UserInfo.getLevel());
        comment.setUserId(UserInfo.getId());
        comment.setUsername(UserInfo.getNickname());

        comment = travelCommentService.save(comment);
        //c
        model.addAttribute("c",comment);

        //floor 楼层
        model.addAttribute("floor",travelCommentService.getCount(comment.getTravelId()));

        return "travel/commentTpl";
    }
}
